module view {
    export class ArsenalView extends eui.Component {
        closeBtn: eui.Button
        strengthenBtn: eui.Button
        equipmentGroup: eui.Group
        equipBtn: eui.Button
        nameLabel: eui.Label
        select: any
        weaponBtn: eui.RadioButton
        headBtn: eui.RadioButton
        footBtn: eui.RadioButton
        bodyBtn: eui.RadioButton
        decorationBtn: eui.RadioButton
        itemInfoLabel: eui.Label
        public constructor() {
            super()
            this.addEventListener(eui.UIEvent.COMPLETE,this.onComplete,this)
            this.skinName = "resource/skin/ArsenalSkin.exml"
        }

        private onComplete(): void {
            this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
            this.equipBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.equip,this)
            this.showEquipment()
            this.showEquipments()
        }

        private close(): void {
            this.parent.removeChild(this)
        }

        private equip(): void {
            var type = this.select.type
            var index = this.select.index
            if(type === 1) {
                game.playerHandle.equip(gplayer,gplayer.bag.type,index)
            } if(type === 2) {
                game.playerHandle.equip(gplayer,gplayer.store.type,index)
            } else if(type === 3) {
                game.playerHandle.unloadEquipment(gplayer,index)
            }
            this.showEquipment()
            this.showEquipments()
        }

        private showEquipment(): void {
            var firstFlag = true
            for(var i in gplayer.equipments) {              
                if( !gplayer.equipments.hasOwnProperty(i)) { 
                    continue
                }
                var btn = this[i + 'Btn']
                if(!gplayer.equipments[i] ){
                    btn.label = ''
                    continue
                }
                var equipment: game.Equipment = gplayer.equipments[i]
               
                btn.label = templateName[equipment.id]
                btn.value = {
                    type: 3,
                    index: i
                }
                btn.addEventListener(eui.UIEvent.CHANGE,this.equipmentTouch,this)
                if(firstFlag) {
                    firstFlag = false
                    this.select = {
                        type: 3,
                        index: i
                    }
                    this.showEquipmentInfo()

                }
            }
            this.strengthenBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.strengthen,this)
        }

        private showEquipments(): void {
            this.equipmentGroup.removeChildren()
            for(var i in gplayer.bag.items) {
                var item = gplayer.bag.items[i]
                if(item && Math.floor(item.id / 10000) === 25) {
                    var btn = new eui.RadioButton
                    btn.skinName = "resource/skin/SmithyRadioBtnSkin.exml"
                    btn.label = templateName[item.id]
                    btn.value = {
                        type: 1,
                        index: i
                    }
                    btn.addEventListener(eui.UIEvent.CHANGE,this.equipmentTouch2,this)
                    btn.groupName = "equipmentGroup"
                    this.equipmentGroup.addChild(btn)
                }
            }

            for(var i in gplayer.store.items) {
                var item = gplayer.store.items[i]
                if(item && Math.floor(item.id / 10000) === 25) {
                    var btn = new eui.RadioButton
                    btn.skinName = "resource/control/SmithyRadioBtnSkin.exml"
                    btn.label = templateName[item.id]
                    btn.value = {
                        type: 2,
                        index: i
                    }
                    btn.addEventListener(eui.UIEvent.CHANGE,this.equipmentTouch2,this)
                    btn.groupName = "equipmentGroup"
                    this.equipmentGroup.addChild(btn)
                }
            }
        }

        private strengthen(evt: egret.TouchEvent): void {
            var type = this.select.type
            var index = this.select.index
            if(type === 1) {
                var equipment = gplayer.bag.items[index]
                game.playerHandle.equipmentLvlup(gplayer,equipment)
            } if(type === 2) {
                var equipment = gplayer.store.items[index]
                game.playerHandle.equipmentLvlup(gplayer,equipment)
            } else if(type === 3) {
                game.playerHandle.equipmentLvlupByPart(gplayer,index)
            }
            this.showEquipmentInfo()
        }

        private equipmentTouch(evt: eui.UIEvent): void {
            var target = evt.target
            this.select = target.value
            this.showEquipmentInfo()
        }

        private equipmentTouch2(evt: eui.UIEvent): void {
            var target = evt.target
            this.select = target.value
            this.showEquipmentInfo()
        }

        private showEquipmentInfo(): void {
            var type = this.select.type
            var index = this.select.index
            var equipment
            if(type === 1) {
                equipment = gplayer.bag.items[index]
                this.equipBtn.label = '装备'
            } else if(type === 2) {
                equipment = gplayer.store.items[index]
                this.equipBtn.label = '装备'
            } else if(type === 3) {
                equipment = gplayer.equipments[index]
                this.equipBtn.label = '卸下'
            }
            var equipmentConfig = totalConfig[equipment.id]
            var strengthenId = equipmentConfig.strengthenId
            var strengthenConfig = totalConfig[strengthenId]
            var need = strengthenConfig[equipment.lvl]
            this.nameLabel.text = equipmentConfig.name
            this.itemInfoLabel.text = '强化等级:' + equipment.lvl + ''
        }
    }
}
